package com.example.zhihuibj.adapter;


import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder {
	private SparseArray<Object> mViews;
	private View mConvertView;
	
	private ViewHolder(Context ctx, int position, ViewGroup parent, int layoutId){
		mConvertView = View.inflate(ctx, layoutId, null);
		mViews = new SparseArray<Object>();
		mConvertView.setTag(this);
	}
	
	public static ViewHolder get(Context ctx, View convertView, int position, ViewGroup parent,int layoutId){
		if (convertView == null){
			return new ViewHolder(ctx ,position, parent,layoutId);
		} else {
			return (ViewHolder) convertView.getTag();
		}
	}
	
	public View getConvertView(){
		return mConvertView;
	}
	
	public View getView(int resId){
		View view = (View) mViews.get(resId);
		
		if (view == null){
			view = mConvertView.findViewById(resId);
		}
		
		return view;
	}
	
	public ViewHolder setText(int resId, String text){
		TextView tv = (TextView) getView(resId);
		tv.setText(text);
		return this;
	}
	
	public ViewHolder setImage(int resId, int imageResId){
		ImageView iv = (ImageView) getView(resId);
		iv.setImageResource(imageResId);
		return this;
	}
}
